const http = require('http');
const fs = require('fs');

const server = http.createServer(); // 构建Server实例

/**
 * 注册 request 事件接受客户端发送的请求
 */
server.on('request', function (request, response) {
  let url = request.url;
  console.log('收到客户端的请求了，请求路径是：' + request.url);
  if (url.endsWith('.png') || url.endsWith('.jpg')) {
    fs.createReadStream('.' + url).pipe(response);
  } else {
    fs.createReadStream('./color_picker.html').pipe(response);
  }
});

// 监听端口
server.listen(3000, function () {
  console.log('服务器启动成功了，可以通过 http://127.0.0.1:3000/ 来进行访问');
});
